#!/bin/bash

# ============================================================================
# 人脸质量检测API - 一键部署脚本
# ============================================================================

set -e

echo "============================================================================"
echo "  人脸质量检测API - Docker部署脚本"
echo "============================================================================"
echo ""

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 检查Docker
echo "📋 检查环境..."
if ! command -v docker &> /dev/null; then
    echo -e "${RED}❌ Docker 未安装${NC}"
    echo "请先安装 Docker: https://docs.docker.com/get-docker/"
    exit 1
fi

if ! command -v docker-compose &> /dev/null; then
    echo -e "${RED}❌ Docker Compose 未安装${NC}"
    echo "请先安装 Docker Compose: https://docs.docker.com/compose/install/"
    exit 1
fi

echo -e "${GREEN}✅ Docker 已安装${NC}"
docker --version
echo -e "${GREEN}✅ Docker Compose 已安装${NC}"
docker-compose --version
echo ""

# 检查模型文件
echo "📦 检查模型文件..."
if [ ! -d "models/buffalo_l" ]; then
    echo -e "${RED}❌ 模型文件不存在: models/buffalo_l/${NC}"
    echo "请将 buffalo_l 模型文件夹放在 models/ 目录下"
    exit 1
fi

MODEL_FILES=(
    "models/buffalo_l/1k3d68.onnx"
    "models/buffalo_l/2d106det.onnx"
    "models/buffalo_l/det_10g.onnx"
    "models/buffalo_l/genderage.onnx"
    "models/buffalo_l/w600k_r50.onnx"
)

for file in "${MODEL_FILES[@]}"; do
    if [ ! -f "$file" ]; then
        echo -e "${YELLOW}⚠️  缺少模型文件: $file${NC}"
    else
        echo -e "${GREEN}✅ $file${NC}"
    fi
done
echo ""

# 检查必要文件
echo "📁 检查必要文件..."
REQUIRED_FILES=(
    "Dockerfile"
    "docker-compose.yml"
    "api.py"
    "config.py"
    "model_manager.py"
    "requirements.txt"
    "face/face_detector.py"
    "static/debug.html"
)

for file in "${REQUIRED_FILES[@]}"; do
    if [ ! -f "$file" ] && [ ! -d "$file" ]; then
        echo -e "${RED}❌ 缺少文件: $file${NC}"
        exit 1
    fi
done
echo -e "${GREEN}✅ 所有必要文件就绪${NC}"
echo ""

# 创建必要目录
echo "📂 创建必要目录..."
mkdir -p uploads logs
echo -e "${GREEN}✅ 目录创建完成${NC}"
echo ""

# 选择操作
echo "请选择操作:"
echo "  1) 构建并启动服务"
echo "  2) 仅构建镜像"
echo "  3) 启动已构建的镜像"
echo "  4) 停止服务"
echo "  5) 重启服务"
echo "  6) 查看日志"
echo "  7) 查看状态"
read -p "请输入选项 [1-7]: " choice

case $choice in
    1)
        echo ""
        echo "🔨 构建并启动服务..."
        echo "============================================================================"
        docker-compose build
        echo ""
        docker-compose up -d
        echo ""
        echo -e "${GREEN}✅ 服务已启动${NC}"
        echo ""
        echo "等待服务就绪..."
        sleep 5
        docker-compose ps
        ;;
    2)
        echo ""
        echo "🔨 构建镜像..."
        echo "============================================================================"
        docker-compose build
        echo ""
        echo -e "${GREEN}✅ 镜像构建完成${NC}"
        ;;
    3)
        echo ""
        echo "🚀 启动服务..."
        echo "============================================================================"
        docker-compose up -d
        echo ""
        echo -e "${GREEN}✅ 服务已启动${NC}"
        sleep 3
        docker-compose ps
        ;;
    4)
        echo ""
        echo "🛑 停止服务..."
        docker-compose down
        echo ""
        echo -e "${GREEN}✅ 服务已停止${NC}"
        ;;
    5)
        echo ""
        echo "🔄 重启服务..."
        docker-compose restart
        echo ""
        echo -e "${GREEN}✅ 服务已重启${NC}"
        sleep 3
        docker-compose ps
        ;;
    6)
        echo ""
        echo "📋 查看日志..."
        echo "============================================================================"
        docker-compose logs -f --tail=100
        ;;
    7)
        echo ""
        echo "📊 服务状态..."
        echo "============================================================================"
        docker-compose ps
        echo ""
        docker stats --no-stream face-quality-api || echo "容器未运行"
        ;;
    *)
        echo -e "${RED}无效选项${NC}"
        exit 1
        ;;
esac

echo ""
echo "============================================================================"
echo "  访问地址"
echo "============================================================================"
echo ""
echo "  🌐 Web界面:  http://localhost:8000"
echo "  📚 API文档:  http://localhost:8000/docs"
echo "  ❤️  健康检查: http://localhost:8000/health"
echo ""
echo "============================================================================"
echo ""
echo "常用命令:"
echo "  查看日志:   docker-compose logs -f"
echo "  停止服务:   docker-compose down"
echo "  重启服务:   docker-compose restart"
echo "  进入容器:   docker-compose exec face-api bash"
echo ""
echo "详细文档请查看: 部署说明.md"
echo ""
echo "============================================================================"

